﻿using System;
using DMS.BaseClass;
using DMS.Business.Modules.Organization;
using DMS.Business.Modules.SystemManagement;
using DMS.Business.Utility;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace DMS.Web.Modules.SystemManagement 
{
    public partial class NoticeEdit : System.Web.UI.Page
    {
        public string serverPath = ConfigurationManager.AppSettings["FileServer"];
        string initContent = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mode = Request["mode"];
                string id = Request["id"];
                if (!String.IsNullOrEmpty(mode) && "view".Equals(mode))
                {
                    ClientHelper.Instance.RenderScript(Page, null, "editor.config.disableReadonlyStyling = true;$('#imgBtn').Css('display', 'none')");
                    Title.ReadOnly = true;
                    Target.Enabled = false;
                    PublishTime.CssClass = "";
                    btnSave.Visible = false;
                }

                DataSet ds = NoticeService.GetALLRoles();
                Target.DataSource = ds;
                Target.DataBind();
                Target.Items.Insert(0, new ListItem("所有人", ""));

                if (!String.IsNullOrEmpty(id))
                {
                    Sys_notice notice = NoticeService.GetNotice(id);
                    PublishTime.Text = notice.PublishTime.GetDateTimeFormats('g')[0];
                    Title.Text = notice.Title;
                    
                    foreach (ListItem item in Target.Items)
                    {
                        if (!String.IsNullOrEmpty(notice.RoleID) && item.Value.ToUpper().Equals(notice.RoleID.ToUpper()))
                        {
                            item.Selected = true;
                            break;
                        }
                    }

                    ContentValue.Value = Server.HtmlDecode(notice.Content);
                    ClientHelper.Instance.RenderScript(Page, "", "init();");
                }
                else
                {
                    PublishTime.Text = DateTime.Now.GetDateTimeFormats('g')[0];
                }
            }
        }

        //保存项目信息
        public void SaveForm(object sender, EventArgs e)
        {
            string id = Request["id"];
            Sys_notice notice = new Sys_notice();
            if(!String.IsNullOrEmpty(Target.SelectedValue))
                notice.RoleID = Target.SelectedValue;
            notice.Title = Title.Text;
            notice.PublishTime = DateTime.Now;
            notice.Deleted = false;

            User user = UserHelper.GetContextUser();
            notice.Publisher = user.Name;
            notice.PublisherID = user.ID.ToString();
            notice.Content = Server.HtmlEncode(ContentValue.Value);
            if(String.IsNullOrEmpty(id)){
                NoticeService.SaveNotice(notice);
            }else{
                notice.ID = new Guid(id);
                NoticeService.UpdateNotice(notice);
            }
            ClientHelper.Instance.RenderScript(Page, "", "window.opener.location = window.opener.location");
            ClientHelper.Instance.CloseWindow("保存成功", "true", RefreshType.NoRefresh);
        }
    }
}